package com.hzw.saas.common.config.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiParam;
import lombok.Data;

@Data
public class PageParam {

    private static final long serialVersionUID = 1660852361265033821L;
    /**
     * 当无需分页时，默认的每页显示条数
     */
    public static final long NO_PAGE_SIZE = 9999;
    public static final long NO_PAGE_CURRENT = 1;

    /**
     * 总数
     */
    @ApiParam(hidden = true)
    private long total = 0;

    /**
     * 每页显示条数，默认 10
     */
    @ApiParam(value = "每页大小，默认10", required = false, defaultValue = "10")
    private long size = 10;

    /**
     * 当前页
     */
    @ApiParam(value = "当前页，默认1", required = false, defaultValue = "1")
    private long current = 1;

    /**
     * 是否进行 count 查询
     */
    @ApiParam(hidden = true)
    private boolean isSearchCount = true;

    public PageParam() {
    }

    public PageParam(long size, long current) {
        this.size = size;
        this.current = current;
    }

    public PageParam(boolean enablePage) {
        if (!enablePage) {
            this.size = NO_PAGE_SIZE;
            this.current = NO_PAGE_CURRENT;
        }
    }

    public long getTotal() {
        return this.total;
    }

    public PageParam setTotal(long total) {
        this.total = total;
        return this;
    }

    @ApiParam(hidden = true)
    public boolean getSearchCount() {
        if (total < 0) {
            return false;
        }
        return isSearchCount;
    }

    @ApiParam(hidden = true)
    public PageParam setSearchCount(boolean isSearchCount) {
        this.isSearchCount = isSearchCount;
        return this;
    }

    public long getSize() {
        return this.size;
    }

    public PageParam setSize(long size) {
        this.size = size;
        return this;
    }

    public long getCurrent() {
        return this.current;
    }

    public PageParam setCurrent(long current) {
        this.current = current;
        return this;
    }

    public static PageParam getBigPageParam() {
        return new PageParam(999999999, 1);
    }

    public <T> Page<T> convert() {
        return new Page<>(this.current, this.size, this.total, this.isSearchCount);
    }
}
